<?php
namespace My\Controller;

use Utuo\BackgroundController;

class GrouponGoodsonController extends BackgroundController {
	
	private $gid = null;
	
	private $status = array(
		'0' => '禁用',
		'1' => '启用',
	);

	public function _initialize() {
		$this->gid = I('gid', 0, "intval");
		$this->setControllerTitle('团购商品管理');
		$this->setActionTitle(array(
			'index' => '商品列表',
			'add' => '商品添加',
			'modify' => '活动配置',
			'recycle' => '回收站',
		), "", 'gid='.$this->gid);
		$this->setActionNav(array(
				'index',
				'add',
				'recycle'
		));
	}
	
	public function indexAction() {
		$this->getLists(0);
	}
	
	/**
	 * 回收站(non-PHPdoc)
	 * @see \Utuo\BackgroundController::recycleAction()
	 */
	public function recycleAction() {
		$this->getLists(1);
	}
	
	/**
	 * 获得团购商品列表
	 * @param unknown $is_deleted
	 */
	public function getLists( $is_deleted ) {
		$page = $this->getPage();
		$pagesize = $this->getPageSize();
		$m = M('GrouponGoodson a')->join("__GROUPON_GOODS__ b on a.goods_id=b.id")->where("a.cid=%d and a.is_deleted=%d and a.groupon_id=%d", $this->aid, $is_deleted, $this->gid)->field("a.*,b.name");
		$n = clone $m;		
		$lists = $m->limit($pagesize)->page($page)->order("id desc")->select();
		$count = $n->count();
		$this->assign('grouponGoodsonLists', $lists);
		$this->showPage($count, $pagesize);
		$this->display();
	}
	
	/**
	 * 添加商品
	 * @see \Utuo\BackgroundController::addAction()
	 */
	public function addAction() {
		redirect(U('GrouponGoods/add', 'gid='.$this->gid));
	}
	
	/**
	 * 修改团购配置
	 */
	public function modifyAction() {
		$model = CONTROLLER_NAME;
		$config = D("GrouponGoodson")->getOneById();
		$config['status'] =  $this->getRadio('status', $this->status, $config['status']);
		$this->assign("grouponGoodson", $config);
		$this->display("save");
	}
	
	
}